// socket.h
#pragma once
#include "../include/commondef.h"
#ifdef WINDOWS
#pragma comment(lib, "Ws2_32.lib")
#endif
#ifdef LINUX
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
typedef struct sockaddr_in SOCKADDR_IN;
#define INVALID_SOCKET (-1)
#endif

int SOCKETAPI_InitSocket(); // 初始化套接字

int SOCKETAPI_CleanSocket(); // 清理套接字

int SOCKETAPI_CloseSocket(SOCKET iSkt); // 关闭套接字

int SOCKETAPI_IsReadable(SOCKET iSkt, int iTime);
int SOCKETAPI_IsWriteable(SOCKET iSkt, int iTime);
/*Above two functions can judge a socket is Readable/Writeable or not.They work via select().

args:
iSkt	--	a socket;
iTime	--	timeout(second),can be 0;

return:
<0	--	socket error;
0	--	timeout ,not Readable/Writeable;
>0	--	Readable/Writeable;

*/

/*send iLen bytes in iTry seconds
args:
iSkt	--	a socket;
data	--	input data;
iLen	--	required length;
iTry	--	retry times;

return:
<0	--	error;
>=0	--	send length;
*/
int SOCKETAPI_Send(SOCKET iSkt, BYTE *data, int iLen, int iTry = 1);

/*recv iLen bytes in iTry seconds
args:
iSkt	--	a socket;
pOut	--	output data;
iLen	--	required length;
iTry	--	retry times;

return:
<0	--	error;
>=0	--	received length;
*/
int SOCKETAPI_Receive(SOCKET iSkt, BYTE *pOut, int iLen, int iTry = 1);

/*TCP accept
iSkt    I/O 输入监听套接字，输出新套接字
pstAddr 输出地址
*/
int SOCKETAPI_Accept(SOCKET &iSkt, OUT SOCKADDR_IN *pstAddr = nullptr);
